package com.example.random_clap.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.random_clap.common.R;
import com.example.random_clap.common.constant.MessageConstant;
import com.example.random_clap.entity.Problem;
import com.example.random_clap.entity.ProblemCategory;
import com.example.random_clap.service.ProblemCategoryService;
import com.example.random_clap.service.ProblemService;
import lombok.extern.slf4j.Slf4j;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.UUID;

/**
 * 问题模块
 *
 * @author ouyangkai
 * @date 2022-10-29
 */
@RestController
@RequestMapping("/problem")
@Slf4j
public class ProblemController {

    @Autowired
    private ProblemService problemService;

    @Autowired
    private ProblemCategoryService problemCategoryService;

    /**
     * 上报问题
     *
     * @param problem
     * @return
     */
    @PostMapping("/reportProblem")
    public R<String> reportProblem(@RequestBody Problem problem) {
        boolean res = problemService.save(problem);
        if (!res) {
            return R.error(MessageConstant.ADD_PROBLEM_ERROR);
        }
        return R.success(MessageConstant.ADD_PROBLEM_SUCCESS);
    }


    /**
     * 分页获取问题列表
     * @param currentPage
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping("getProblem")
    public R<Page<Problem>> getProblem(int currentPage, int pageSize, String name){

        Page<Problem> pageInfo = new Page<>(currentPage,pageSize);

        LambdaQueryWrapper<Problem> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Strings.isNotBlank(name),Problem::getName, name);

        problemService.page(pageInfo,queryWrapper);

        if(pageInfo.getRecords().size() == 0){
            return R.error(MessageConstant.QUERY_PROBLEM_ERROR);
        }
        return R.success(pageInfo);
    }


    /**
     * 获取我上传的问题 (跟踪进度）
     * @param userId
     * @return 当前用户所有上传的问题
     */
    @GetMapping("/getProblemByUserId")
    public R<List<Problem>> getProblemByUserId(Long userId){
        LambdaQueryWrapper<Problem> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Problem::getUserId, userId);
        List<Problem> problemList = problemService.list(queryWrapper);

        return R.success(problemList);
    }

    /**
     * 审核问题
     * @param problem
     * @return
     */
    @PutMapping("/auditProblem")
    public R<String> auditProblem(Problem problem){
        LambdaQueryWrapper<Problem> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Problem::getId,problem.getId());
        boolean res = problemService.update(problem,queryWrapper);
        if(!res){
            return R.error(MessageConstant.AUDIT_PROBLEM_ERROR);
        }
        return R.success(MessageConstant.AUDIT_PROBLEM_SUCCESS);
    }

    /**
     * 处理问题
     * @param problem
     * @return
     */
    @PutMapping("/handleProblem")
    public R<String> handleProblem(Problem problem){
        LambdaQueryWrapper<Problem> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Problem::getId,problem.getId());
        boolean res = problemService.update(problem,queryWrapper);
        if(!res){
            return R.error(MessageConstant.HANDLE_PROBLEM_ERROR);
        }
        return R.success(MessageConstant.HANDLE_PROBLEM_SUCCESS);
    }

    @PutMapping("/FinishHandleProblem")
    public R<String> FinishHandleProblem(Problem problem){
        LambdaQueryWrapper<Problem> queryWrapper = new LambdaQueryWrapper<>();

        queryWrapper.eq(Problem::getId,problem.getId());
        boolean res = problemService.update(problem,queryWrapper);
        if(!res){
            return R.error(MessageConstant.HANDLE_PROBLEM_ERROR);
        }
        return R.success(MessageConstant.HANDLE_PROBLEM_SUCCESS);
    }



    /**
     * 查看问题详情
     * @return
     */
    @GetMapping("/getProblemById")
    public R<Problem>  getProblemById(Long problemId){
        Problem problem = problemService.getById(problemId);

        if(problem == null){
            return R.error(MessageConstant.QUERY_PROBLEM_DETAIL_ERROR);
        }

        return R.success(problem);
    }

    /**
     * 上传图片
     * @return
     */
    @PostMapping("/upload")
    public R<String> upload(MultipartFile file){
        String originalFilename = file.getOriginalFilename();
        String suffix = originalFilename.substring(originalFilename.lastIndexOf("."));

        String fileName = UUID.randomUUID().toString() + suffix;

        log.info(fileName);

        return R.success(fileName);
    }


    /**
     * 获取问题列表
     * @return
     */
    @GetMapping("/getNoAuditProblem")
    public R<Page<Problem>> getNoAuditProblem(int currentPage, int pageSize,int status){
        Page<Problem> pageInfo = new Page<>(currentPage,pageSize);
        LambdaQueryWrapper<Problem> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Problem::getStatus,status);
        problemService.page(pageInfo,queryWrapper);

        return R.success(pageInfo);
    }
}
